package com.efei.jdbc.sql;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.List;

@Setter
@Getter
public class SqlSelect extends SqlCommon{

    private StringBuilder select  = new StringBuilder();

    private StringBuilder from = new StringBuilder();

    private StringBuilder where = new StringBuilder();

    private StringBuilder orderBy = new StringBuilder();

    private StringBuilder groupBy = new StringBuilder();

    private StringBuilder having = new StringBuilder();

    //private List<SqlCondition> conditions = null;

    SqlSelect() {
        super(SqlType.SELECT);
    }

    public static SqlSelect of() {
        return new SqlSelect();
    }

    public SqlSelect select(String col) {
        if(StringUtils.hasLength(col)) {
            return this;
        }
        if(this.select.length()>0) {
            this.select.append(",");
        }
        this.select.append(col);
        return this;
    }

    public SqlSelect from(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(this.from.length()>0) {
            this.from.append(",");
        }
        this.from.append(tb);
        return this;
    }

    public SqlSelect where(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        //this.where.append(" ");
        this.where.append(tb);
        return this;
    }

    public SqlSelect whereAnd(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(where.length()>0) {
            this.where.append(" and ");
        }
        this.where.append(tb);
        return this;
    }

    public SqlSelect whereAnd(SqlCondition c) {
        if(where.length()>0) {
            this.where.append(" and ");
        }
        //conditions.add(c);
        this.where.append(c.getColumn());
        this.where.append(c.getOperator());
        String alis = c.getAlis();
        this.where.append(":"+alis);
        this.addParameter(alis, c.getValue());
        return this;
    }

    public SqlSelect whereOr(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(where.length()>0) {
            this.where.append("or");
        }
        this.where.append(tb);
        return this;
    }

    public SqlSelect whereOr(SqlCondition c) {
        if(where.length()>0) {
            this.where.append("or");
        }
        //conditions.add(c);
        this.where.append(c.getColumn());
        this.where.append(c.getOperator());
        String alis = c.getAlis();
        this.where.append(":"+alis);
        this.addParameter(alis, c.getValue());
        return this;
    }

    public SqlSelect orderBy(String oy) {
        if(StringUtils.hasLength(oy)) {
            return this;
        }
        this.orderBy.append(oy);
        return this;
    }

    public SqlSelect group(String gp) {
        if(StringUtils.hasLength(gp)) {
            return this;
        }
        this.groupBy.append(gp);
        return this;
    }

    public SqlSelect having(String hv) {
        if(StringUtils.hasLength(hv)) {
            return this;
        }
        this.having.append(hv);
        return this;
    }

    @Override
    public StringBuilder getSql() {
        StringBuilder sql = new StringBuilder();
        sql.append("select ").append(select).append(" from ").append(from);
        if(where.length()>0) {
            sql.append(" where ").append(where);
        }
        if(groupBy.length()>0) {
            sql.append(" group by ").append(groupBy);
        }
        if(having.length()>0) {
            sql.append(" having ").append(having);
        }
        if(orderBy.length()>0) {
            sql.append(" order by ").append(orderBy);
        }
        return sql;
    }
}
